package com.tsing.basic.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsing.basic.entity.User;
import com.tsing.basic.vo.dto.CarportTreeChildrenDTO;
import com.tsing.basic.vo.dto.HouseTreeChildren2DTO;
import com.tsing.basic.vo.dto.HouseTreeChildrenDTO;
import com.tsing.basic.vo.req.BindRoleReqVo;
import com.tsing.basic.vo.req.UserPageReqVo;
import com.tsing.basic.vo.resp.CarportTreeRespVo;
import com.tsing.basic.vo.resp.HouseTreeRespVo;
import com.tsing.basic.vo.resp.RoleRespVo;
import com.tsing.basic.vo.resp.UserRespVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author TheTsing
 * @since 2021-03-29
 */
public interface UserMapper extends BaseMapper<User> {

    @Select("select u.*\n" +
            "from t_user u\n" +
            "where u.id=#{id}")
    UserRespVo selectUserById(@Param("id") String id);

    @Select("select distinct ur.role_id\n" +
            "from t_user_role ur\n" +
            "inner join t_role r on ur.role_id=r.id\n" +
            "where ur.user_id=#{id}")
    List<String> selectRoleId(@Param("id") String id);

    @Select("<script>" +
            "select u.*,house_id houseId,carport_id carportId,\n" +
            "h.name houseName,un.name unitName,b.name buildingName,c.name carportName,d.name depotName\n" +
            "from t_user u\n" +
            "left join t_house h on h.id=u.house_id\n" +
            "left join t_unit un on un.id=h.unit_id\n" +
            "left join t_building b on b.id=un.building_id\n" +
            "left join t_carport c on c.id=u.carport_id\n" +
            "left join t_depot d on d.id=c.depot_id\n" +
            "<where>\n" +
            "<if test=\"reqVo.nickName!=null and reqVo.nickName!=''\">and u.nick_name like concat('%',#{reqVo.nickName},'%')</if>\n" +
            "<if test=\"reqVo.phone!=null and reqVo.phone!=''\">and u.phone like concat('%',#{reqVo.phone},'%')</if>\n" +
            "</where>\n" +
            "</script>")
    List<UserRespVo> userPage(UserPageReqVo reqVo, Page<User> page);

    @Delete("delete from t_user_role where user_id=#{id}")
    void deleteRelevance(@Param("id") String id);

    @Select("select r.id,r.name,r.description remark\n" +
            "from t_user_role ur\n" +
            "inner join t_role r on ur.role_id=r.id\n" +
            "where ur.user_id=#{id}")
    List<RoleRespVo> getRoleById(@Param("id") String id);

    @Insert("<script>" +
            "insert into t_user_role(user_id,role_id) values\n" +
            "<foreach collection='reqVo.roleId' item='item' separator=',' >\n" +
            "( #{reqVo.id} , #{item} )\n" +
            "</foreach>\n" +
            "</script>")
    void bindRole(@Param("reqVo") BindRoleReqVo reqVo);

    @Select("select id,name label from t_building")
    @Results({
            @Result(property = "children", column = "id", many = @Many(select = "getHouseTreeChildren"))
    })
    List<HouseTreeRespVo> getHouseTree();

    @Select("select id,name label from t_unit where building_id=#{id}")
    @Results({
            @Result(property = "children", column = "id", many = @Many(select = "getHouseTreeChildren2"))
    })
    List<HouseTreeChildrenDTO> getHouseTreeChildren(@Param("id") String id);

    @Select("select id houseId,name label from t_house where unit_id=#{id}")
    List<HouseTreeChildren2DTO> getHouseTreeChildren2(@Param("id") String id);

    @Select("select id,name label from t_depot")
    @Results({
            @Result(property = "children", column = "id", many = @Many(select = "getCarportTreeChildren"))
    })
    List<CarportTreeRespVo> getCarportTree();

    @Select("select id carportId,name label from t_carport where depot_id=#{id}")
    List<CarportTreeChildrenDTO> getCarportTreeChildren(@Param("id") String id);

}
